package com.fanbai.score.common.Utils.pop;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import com.fanbai.score.R;
import com.fanbai.score.common.Utils.ScreenUtils;

import java.util.List;

public class PopListWin {


	private Activity ctx;
	private View view;
	private PopupWindowUtils popWin;
//	private TextView tvNew, tvPhoto, tvCancle;
	private ListView listView;
	private OnSexClickListener onSexClickListener;

	/**
	 * 构造
	 *
	 * @param ctx
	 */
	public PopListWin(Activity ctx) {
		this.ctx = ctx;
		initView();
		if (popWin == null)
			popWin = new PopupWindowUtils(ctx).setContentView(view);
	}

	/**
	 * 显示
	 * 
	 * @param parent
	 */
	public void show(View parent) {
		popWin.show(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
	}


	private void initView() {
		view = View.inflate(ctx, R.layout.pop_menu_other, null);
		listView = (ListView) view.findViewById(R.id.popMenuOther_listview);
	}

	public void setListData(final List<String> data){
		listView.setAdapter(new ArrayAdapter<String>
				(ctx, R.layout.pop_menu_other_item,data));
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if(data.size()-1 > position){
					if(onSexClickListener == null){
						throw new NullPointerException("onSexClickListener is null");
					}
					onSexClickListener.OnSexClickListener(true, ""+data.get(position), position);
				}else{
					onSexClickListener.OnSexClickListener(false, "", -1);
				}
				popWin.dismiss();
			}
		});
		int scrheight = ScreenUtils.getScreenHeight()  / 3;
		if(data.size() * 45 > scrheight){
			popWin.setHeight(scrheight);
		}
	}


	public void setOnSexClickListener(PopListWin.OnSexClickListener onSexClickListener) {
		this.onSexClickListener = onSexClickListener;
	}

	/**
	 * 性别选择对话框的回调方法
	 */
	public interface OnSexClickListener{
		/**
		 * 性别选择对话框的回调方法
		 * @param b 是否选择
         * @param sex 选择后的性别
         */
		public void OnSexClickListener(boolean b, String sex, int index);
	}
}
